#!/bin/bash

if [[ $# -lt 5 ]]; then
    echo "Please specify arguments as below, usage: download_from_gitee.sh <repo-name> <gitee-username> <gitee-password> <pull-id> <branch-name>"
    exit 1
fi
#$giteeTargetRepoName
repo_name=$1
#$GiteeCloneUserName
gitee_username=$2
#$GiteeClonePassword
gitee_password=$3
#$giteePullRequestIid
pull_id=$4
#$giteeBranch
branch_name=$5


if [[ ! ${repo_name}  ]]; then
  echo "exit when target repository name is null"
  exit 1
else
  echo "current folder is: "
  pwd
  if [[ -d ${repo_name} ]]; then
    # clean up the folder, branch and pull master into latest status.
    cd ${repo_name}
    git checkout -b dogsheng
    git checkout -- .
    git clean -f
    git checkout $branch_name
    git branch | grep -ve $branch_name | xargs git branch -D
  else
    if [[ ${repo_name} == "kernel" ]]  || [[ ${repo_name} == "texlive" ]]; then
        sshpass -p "8JyjvPSjpS#FXCX6Ud" scp -o StrictHostKeyChecking=no -r root@172.16.1.89:/srv/cache/obs/tar_scm/repo/next/openEuler/${repo_name} .
        cd ${repo_name}
        #force update gitconfig to use https instead of ssh
        sed -i "s/git@gitee.com:/https:\/\/gitee.com\//g" .git/config
        git checkout -b dogsheng
        git checkout -- .
        git clean -f
        git checkout ${branch_name}
        git branch | grep -ve ${branch_name} | xargs git branch -D
    else
        git clone "https://${gitee_username}:${gitee_password}@gitee.com/src-openeuler/${repo_name}.git"
        cd ${repo_name}
    fi
  fi
  
  git fetch origin pull/${pull_id}/head:${branch_name}-${pull_id}
  git checkout ${branch_name}-${pull_id}
  git --no-pager diff --name-only ${branch_name}-${pull_id} $(git merge-base ${branch_name}-${pull_id} master) > ../changefilelist
fi
